from flask import Blueprint, request

from app.Controllers.BaseController import BaseController
from app.Models.Compounds import Compounds
from app.Models.Reaction import Reaction
from app.Models.ReactionPairs import ReactionPairs
from app.Service.SteinterTreeService import SteinterTreeService

st = Blueprint('steinter', __name__)


@st.route('/')
def index():
    return BaseController().successData(msg="it's steinter")


@st.route('/getSteinterTreePathway', methods=['POST'])
def getSteinterTreePathway():
    # 对前端传输来的数据进行处理，分割，缩减等
    text = request.json["textarea"]
    terminal_nodes_name = SteinterTreeService().splitText(text)
    # 调用算法获取所有cpd的名字列表和反应对的列表
    cpdName_list, interaction_list = SteinterTreeService().getPathway(terminal_nodes_name)
    # 从compound的表中查询包含的cpd位点坐标,查询出对应的cpd的坐标，对应成{Cxxxxx:data-coords}
    cpdImformations = Compounds().getList({
        Compounds.data_entry.in_(cpdName_list)
    })
    # 从reaction_pairs的表中查询包含的reaction路径位点坐标,并且通过边和点的双向比对，获取确定存在的边和点的列表
    # 查询出对应的pairs的Rxxxxx结果，输出为reactions ->（Rxxxxx, Cxxxxx, Cxxxxx）
    reaction_list = ReactionPairs().getList(interaction_list, cpdImformations)
    # 将可能查询到的路径,并且通过边和点的双向比对，获取确定存在的边和点的列表
    # 查询reactions的路径坐标，并根据Cxxxxx的位置的坐标去验证Rxxxxx的通路是否存在, 将成功验证的边和点输出成reactions_list和cpd_list，将interaction的hashtable置1
    trueReactionList, trueCpdList, interaction_list, maybe_node = Reaction().getList(reaction_list, cpdImformations, interaction_list)
    # 把得到的结果返还
    result = dict(trueReactionList=trueReactionList, trueCpdList=trueCpdList, least_interaction_list=interaction_list, least_cpd_list=maybe_node)
    return BaseController().successData(data=result)
